/*
 * Copyright (C) 2019 ByteDance Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bytedance.scene.ui;


import com.bytedance.scene.Scene;
import com.bytedance.scene.group.GroupScene;
import com.bytedance.scene.group.UserVisibleHintGroupScene;
import com.bytedance.scene.ui.bottomnavigation.BottomNavigationBar;
import com.bytedance.scene.ui.bottomnavigation.BottomNavigationItem;
import com.bytedance.scene.ui.tab.DynamicPagerIndicator;
import com.bytedance.scene.ui.template.ItemInfo;
import com.bytedance.scene.ui.view.DynamicFragmentPagerAdapter;
import ohos.aafwk.ability.Ability;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.PageSlider;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Set;

/**
 * Created by JiangQi on 8/16/18.
 */
public class GroupSceneUIUtility {
    public final static int TEXT_SIZE = 50;
    public static void setupWithBottomNavigationView(BottomNavigationBar mBottomNavigationView, GroupScene groupScene,
                                                     int id_scene_container, LinkedHashMap<String, Scene> sceneMap, List<ItemInfo> tabList, Ability ability) {

        if (sceneMap.size() == 0) {
            throw new IllegalArgumentException("children can't be empty");
        }
        if(tabList.size() == 0){
            throw new IllegalArgumentException("children Info can't be empty");
        }

                for(int i=0;i<tabList.size();i++){
                    mBottomNavigationView.addItem(
                            new BottomNavigationItem(
                                    tabList.get(i).getIcon(),tabList.get(i).getTabName(), ability)
                                    .setActiveColor(tabList.get(i).getColor())
                    );
                }
        mBottomNavigationView.setFirstSelectedPosition(0)
                .initialise();



        final List<String> menuIdList = new ArrayList<>(sceneMap.keySet());

        String tag = menuIdList.get(0);
        Scene scene = groupScene.findSceneByTag(tag);
        if (scene == null) {
            scene = sceneMap.get(tag);
        }
        if (!groupScene.isAdded(scene)) {
            groupScene.add(id_scene_container, scene, tag);
        } else if (!groupScene.isShow(scene)) {
            groupScene.show(scene);
        }

        mBottomNavigationView.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {

                String tag = menuIdList.get(position);
                Scene scene = groupScene.findSceneByTag(tag);
                if (scene == null) {
                    scene = sceneMap.get(tag);
                }
                if (!groupScene.isAdded(scene)) {
                    groupScene.add(id_scene_container, scene, tag);
                } else if (!groupScene.isShow(scene)) {
                    groupScene.show(scene);
                }
                for (int i = 0; i < menuIdList.size(); i++) {
                    Scene otherScene = groupScene.findSceneByTag(menuIdList.get(i));
                    if (otherScene != null && otherScene != scene && groupScene.isAdded(otherScene) && groupScene.isShow(otherScene)) {
                        groupScene.hide(otherScene);
                    }
                }
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });
    }


    public static void setupWithViewPager(PageSlider viewPager7, DynamicPagerIndicator dynamicPagerIndicator7,
                                          int index, boolean isChange, GroupScene groupScene, LinkedHashMap<String, UserVisibleHintGroupScene> list, Ability ability) {
        setViewPagerContent(viewPager7, dynamicPagerIndicator7, index,list,true,ability,groupScene);
    }

    private static void setViewPagerContent(PageSlider viewPager, DynamicPagerIndicator dynamicPagerIndicator,
                                            int index, LinkedHashMap<String, UserVisibleHintGroupScene> list, boolean isChange, Ability ability, GroupScene groupScene) {
        List<UserVisibleHintGroupScene> fragments = createFragments(index,ability,list);
        List<String> ll = new ArrayList<>(list.keySet());

        ScenePageAdapter scenePageAdapter = new ScenePageAdapter(groupScene, fragments, ll);
//        DynamicFragmentPagerAdapter adapter = new DynamicFragmentPagerAdapter(fragments);
        viewPager.setProvider(scenePageAdapter);
        if (isChange) {
            viewPager.setCurrentPage(1);
        } else {
            viewPager.setCurrentPage(0);
        }
        dynamicPagerIndicator.setViewPager(viewPager);
        ability.getUITaskDispatcher().delayDispatch(new Runnable() {
            @Override
            public void run() {
                /**
                 * 默认选中第一个
                 */
                viewPager.setCurrentPage(0);
            }
        }, 0);
    }

    private static List<UserVisibleHintGroupScene> createFragments(int index, Ability ability, LinkedHashMap<String, UserVisibleHintGroupScene> children) {

        List<String> titleList = new ArrayList<>(children.keySet());
        final List<UserVisibleHintGroupScene> sceneList = new ArrayList<>();
        for (String key : titleList) {
            sceneList.add(children.get(key));
        }

//        List<DirectionalLayout> fragments = new ArrayList<>();
//        for (int subscript = 0; subscript < index; subscript++) {
//            fragments.add(createDirectionalLayout(subscript,ability));
//        }
        return sceneList;
    }

    private static DirectionalLayout createDirectionalLayout(int index, Ability ability) {
        DirectionalLayout directionalLayout = new DirectionalLayout(ability);
        DirectionalLayout.LayoutConfig layoutConfig =
                new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_PARENT,
                        DirectionalLayout.LayoutConfig.MATCH_PARENT);
        layoutConfig.alignment = LayoutAlignment.CENTER;
        directionalLayout.setLayoutConfig(layoutConfig);
        Text textView = new Text(ability);
        textView.setTextAlignment(TextAlignment.CENTER);
        textView.setTextSize(TEXT_SIZE);
        textView.setLayoutConfig(layoutConfig);
        directionalLayout.addComponent(textView);
        textView.setText(String.format(Locale.ENGLISH, "child Scene #%d", index));
        return directionalLayout;
    }
}
